package nio;

import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author wlz
 * @date 2022-07-06  10:58 下午
 */
@Slf4j
public class MyAIOClient {

    public static void main(String[] args) throws Exception{
        AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();

        socketChannel.connect(new InetSocketAddress("localhost", 9000)).get();

        socketChannel.write(ByteBuffer.wrap("helloserver".getBytes(StandardCharsets.UTF_8)));

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        Integer len = socketChannel.read(buffer).get();
        if (len > 0) {
            log.info("客户端接收到的消息: {}", new String(buffer.array(),0,len));
        }

    }
}
